<?php
/**
 * Project:     itoFW: Ito PHP Framework
 * File:        system.php
 *
 * Main itoFW system file with some helpfull functions and components, modules and bots loaders and connectors.
 * 
 * @package itoFW
 * @version 1.0.0
 * @changes none
 */

if (!defined('_ito_sys')) die('Neturite leidimo perziureti sia svetaines dali.');

//Loadinamas configas
require_once('configuration.php');
global $conf;

//Loadinamas loaderis x)
require_once('itoLoader.php');

/**
 * Konfiguracijos gavimas
 *
 * @param string $item Gali buti: group_item arba item. Pirmu atveju grazins nustatyma, antru visa grupe array
 * @return mix
 * @todo Sukurus error sistema, iterpti error f-ja jei $itemCnt > 2
 */
function getConf($item) {
	global $conf;
	//Padarome $item array
	$data = '';
	$item = explode('_',$item);
	$itemCnt = count($item);

	if ($itemCnt == 1) {
		$data = $conf[$item[0]];
	} elseif ($itemCnt == 2) {
		$data = $conf[$item[0]][$item[1]];
	} else {
		//Padaryti error reportinima.
		die('Neteisinga uzklausa konfigo gavimui: '.$item);
	}
	
	return $data;
}

/**
 * Returns formated public variables or part of them.
 *
 * @param string $type Can be: get, post or file
 * @param string $item Name of item element to return. Leave it empty for all
 * @param array $options Options what rules to use for data formating
 * @return mix Returns data or null
 * 
 * @todo Sukurti validation rules ir kai bus sukurta bendroji error handlinimo sistema implementuoti ja cia.
 */
function getPubData($typeItem,$options='') {
	$data = null;
	
	$typeItem = explode('_',$typeItem);
	$cnt = count($typeItem);
    
	if ($typeItem[0] == 'get') {
	    $data = ($cnt == 1) ? isset($_GET) ? $_GET : ''  : isset($_GET[$typeItem[1]]) ? $_GET[$typeItem[1]] : '';
	} elseif ($typeItem[0] == 'post') {
	    $data = ($cnt == 1) ? (isset($_POST) ? $_POST : '')  : (isset($_POST[$typeItem[1]]) ? $_POST[$typeItem[1]] : '');
	} elseif ($typeItem[0] == 'files') {
	    $data = ($cnt == 1) ? isset($_FILES) ? $_FILES : ''  : isset($_FILES[$typeItem[1]]) ? $_FILES[$typeItem[1]] : '';
	}
		
	return $data;
}
/**
 * Redirecter
 *
 * @param string $url URL to redirect
 */
function redirectIt($url) {

        if (headers_sent()) echo "<script>document.location.href='$url';</script>\n";
        else {
            header( "Location: $url" );
        }
        exit(); 
}

?>